class Personal_data {
	constructor() {}
	Axios(url, fordata, callback) {
		axios
			.post(url, fordata, {
				headers: {
					TOKEN: localStorage.getItem("token"),
					SAT: "1234567",
				},
			})
			.then((result) => {
				let { verify, msg, member } = result.data;
				if (verify) {
					if (typeof callback === "function") {
						callback(result, this);
					}
				} else {
					console.log(msg);
				}
			})
			.catch((err) => console.log(err));
	}
	getLocalTime(nS) {
		return new Date(parseInt(nS)).toLocaleString().replace(/:\d{1,2}$/, " ");
	}
	chage() {
		let user = JSON.parse(localStorage.getItem("user"));

		let head_portrait = document.querySelector(".head_portrait").children[0];

		head_portrait.src = user.useravatar;
		let nickname = document.querySelector("#nickname");
		nickname.innerHTML = user.nickname;
		let phone = document.querySelector("#phone");
		phone.innerHTML = user.phone;
		let email = document.querySelector("#email");
		email.innerHTML = user.email;
	}
	bangding(callback) {
		let qaq = null;
		document.querySelector(".change_head").addEventListener("click", () => {
			document.querySelector("#change_head").click();
		});
		document.querySelector("#change_head").addEventListener("change", function () {
			let reader = new FileReader();
			reader.onloadend = function (e) {
				qaq = e.target.result;
				if (typeof callback === "function") {
					callback(qaq);
				}
			};
			reader.readAsDataURL(this.files[0]);
		});
	}
	Axios2(
		url,
		method,
		data = {},
		params = {},
		{ headers = { SAT: "1234567" } },
		callback = () => {}
	) {
		axios({
			url,
			method,
			data,
			params,
			headers,
		})
			.then((result) => {
				callback(result);
			})
			.catch((err) => console.log(err));
	}
}
let personal_data = new Personal_data();

personal_data.chage();

personal_data.bangding((res) => {
	let formd = new FormData();
	formd.append("base64", res);
	formd.append("userid", JSON.parse(localStorage.getItem("user")).id);
	personal_data.Axios("https://ty.php800.cn/api/member/avatar.php", formd, (qwq) => {
		document.querySelector("#image").src = qwq.data.dataPath;
	});
});
personal_data.Axios2(
	"https://ty.php800.cn/api/article/list.php",
	"get",
	"",

	{
		userid: JSON.parse(localStorage.getItem("user")).id,
	},

	"",
	(res) => {
		let dynamic = document.querySelector(".Dynamic").children[0];
		let { verify, msg, blogs } = res.data;
		if (verify) {
			for (let i = blogs.length - 1; i > 0; i--) {
				const item = blogs[i];
				personal_data.Axios2(
					"https://ty.php800.cn/api/member/search.php",
					"get",
					{},
					{
						id: item.userid,
					},
					{
						headers: {
							TOKEN: localStorage.getItem("token"),
							SAT: "1234567",
						},
					},
					(res) => {
						let { verify, msg, member } = res.data;
						if (verify) {
							let li = document.createElement("li");
							li.innerHTML = `
								<dl>
									<dt>
										<img
											src="${member[0].useravatar}"
										/>
									</dt>
									<dd>
										<div class="name_time">
											<p>${member[0].nickname}</p>
											<span>54分钟前 来自 皮皮时光机</span>
										</div>
										<span>1天前</span>
									</dd>
								</dl>
								<p>${item.title}</p>
								<div class="like">
									<span><i class="iconfont">&#xe602;</i>点赞</span>
									<span><a href='article_details.html?id=${item.id}'><i class='iconfont'>&#xe612;</i>详情</a></span>
								</div>
							`;
							dynamic.appendChild(li);
						} else {
							console.log(msg);
						}
					}
				);
			}
		} else {
			console.log(msg);
		}
	}
);
document.querySelector("#exit").addEventListener("click", () => {
	location.href = "change_password.html";
});
